var md5=require('md5');
var xhrutils=require('xhrutils');
var ajax=require('ajax');
cc.Class({
    extends: cc.Component,

    properties: {
        b1:{
            default:null,
            type:cc.Node
        },
        b2:{
            default:null,
            type:cc.Node
        },
        frame:{
            default:null,
            type:cc.Sprite
        },
        login:{
            default:null,
            type:cc.Node
        }
        // foo: {
        //    default: null,      // The default value will be used only when the component attaching
        //                           to a node for the first time
        //    url: cc.Texture2D,  // optional, default is typeof default
        //    serializable: true, // optional, default is true
        //    visible: true,      // optional, default is true
        //    displayName: 'Foo', // optional
        //    readonly: false,    // optional, default is false
        // },
        // ...
    },

    // use this for initialization
    onLoad: function () {
        // xhrutils.noop();
        // ajax.getJSON('http://192.168.0.118/res/import/d6/d6788d50-18ce-46c8-95f2-3f9e254b152a.json',null,function(data){
        //     cc.log(data);
        //     }
        // );
        
        var self=this;
        this.b1.on(cc.Node.EventType.TOUCH_END,function(e){
             if(cc.sys.isNative){
       // jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "showAlertDialog", "(Ljava/lang/String;Ljava/lang/String;)V", "voice", "voice1");
             }
         var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function () {
        if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
         var response = xhr.responseText;
         cc.log(xhr);
      //   jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "showAlertDialog", "(Ljava/lang/String;Ljava/lang/String;)V", "voice", "voice");
         
        }
        };
        xhr.open("GET", 'http://192.168.0.118', true);
        xhr.send();
       
        });
       
        this.b2.on(cc.Node.EventType.TOUCH_END,function(e){
            var a=[{s:'s'},{b:{k:'dd',sf:'fsdf'}}];
            //操作JSON
            jsb.fileUtils.writeStringToFile(JSON.stringify(a),'a.json');
            cc.loader.load('http://192.168.0.118/indexbj.jpg', function (err, tex) {
            });
        });
        
        this.login.on(cc.Node.EventType.TOUCH_END,function(e){
            ajax.getJSON('http://192.168.0.167:8080/login/login',{'username':'lisen','password':'1'},function(data){
                if(data.user_name){
                    cc.log(data.user_name+'登录了');
                    cc.director.loadScene("menu");
                }
            });
        });
        
     //   cc.log(jsb.fileUtils.getWritablePath());
    var loadNative = function(url, callback){
    var dirpath =  jsb.fileUtils.getWritablePath() + 'img/';
    var filepath = dirpath  +md5(url) + '.jpg'; 

    function loadEnd(){
        cc.loader.load(filepath, function(err, tex){
            if( err ){
                cc.error(err);
            }else{
                var spriteFrame = new cc.SpriteFrame(tex);
                if( spriteFrame ){
                    spriteFrame.retain();
                    callback(spriteFrame);
                }
            }
        });

    }

    if( jsb.fileUtils.isFileExist(filepath) ){
        cc.log('Remote is find' + filepath);
        loadEnd();
        return;
    }

    var saveFile = function(data){
        if( typeof data !== 'undefined' ){
            if( !jsb.fileUtils.isDirectoryExist(dirpath) ){
                jsb.fileUtils.createDirectory(dirpath);
            }

            if( jsb.fileUtils.writeDataToFile(  new Uint8Array(data) , filepath) ){
                cc.log('Remote write file succeed.');
                loadEnd();
            }else{
                cc.log('Remote write file failed.');
            }
        }else{
            cc.log('Remote download file failed.');
        }
    };
    
    var xhr = new XMLHttpRequest();

    xhr.onreadystatechange = function () {
        cc.log("xhr.readyState  " +xhr.readyState);
        cc.log("xhr.status  " +xhr.status);
        if (xhr.readyState === 4 ) {
            if(xhr.status === 200){
                xhr.responseType = 'arraybuffer';
                saveFile(xhr.response);
            }else{
                saveFile(null);
            }
        }
    }.bind(this);
    xhr.open("GET", url, true);
    xhr.send();
};
var img_url='http://forum.cocos.com//images/logo.png';//'http://192.168.0.118/indexbj.jpg'
    if(cc.sys.isNative){
        xhrutils.loadNativeImg(img_url,function(sf){
            self.frame.spriteFrame.setTexture(sf);
        });
    }

    },

    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});
